package cn.edu.zafu.bigdata.x.zbw;

import java.io.*;
import java.net.*;

public class TCPChat {
    public static void main(String[] args) {
        if (args.length == 0) {
            System.out.println("Usage: java TCPChat [server|client]");
            return;
        }

        if (args[0].equals("server")) {
            server();
        } else if (args[0].equals("client")) {
            if (args.length < 3) {
                System.out.println("Usage: java TCPChat client <host> <port>");
                return;
            }
            client(args[1], Integer.parseInt(args[2]));
        } else {
            System.out.println("Usage: java TCPChat [server|client]");
        }
    }

    public static void server() {
        try (ServerSocket serverSocket = new ServerSocket(9999)) {
            System.out.println("等待客户端连接...");
            while (true) {
                Socket clientSocket = serverSocket.accept();
                System.out.println("客户端已连接: " + clientSocket.getInetAddress());
                new Thread(() -> handleClient(clientSocket)).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void client(String host, int port) {
        try (Socket socket = new Socket(host, port)) {
            System.out.println("连接到服务端: " + host + ":" + port);
            new Thread(() -> readFromServer(socket)).start();
            writeToServer(socket);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void handleClient(Socket socket) {
        try (BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
             PrintWriter out = new PrintWriter(socket.getOutputStream(), true)) {
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                System.out.println("客户端: " + inputLine);
                out.println(inputLine);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void readFromServer(Socket socket) {
        try (BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                System.out.println("服务端: " + inputLine);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void writeToServer(Socket socket) {
        try (BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
             PrintWriter out = new PrintWriter(socket.getOutputStream(), true)) {
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                if ("exit".equalsIgnoreCase(inputLine)) {
                    break;
                }
                out.println(inputLine);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
